/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.restclient.dialogs;

import cz.muni.fi.pa165.machineryrental.restclient.enums.MachineSearchType;

/**
 *
 * @author jostri
 */
public class MachineSearchByDialog extends javax.swing.JDialog {

	private boolean dialogResult;
	private String value;
	
	/**
	 * Creates new form MachineSearchByDialog
	 */
	public MachineSearchByDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}
	
	public boolean getDialogResult() {
		return this.dialogResult;
	}
	
	public String getSearchValue() {
		return this.value;
	}
	
	public MachineSearchType getSearchType() {
		if (this.searchByCB.getSelectedIndex() == 0) {
			return MachineSearchType.Code;
		} 
		else if (this.searchByCB.getSelectedIndex() == 1) {
			return MachineSearchType.Model;
		}
		else {
			return MachineSearchType.Brand;
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        searchByLbl = new javax.swing.JLabel();
        searchByCB = new javax.swing.JComboBox();
        valueLbl = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        valueTB = new javax.swing.JTextPane();
        searchBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        errorLbl = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        searchByLbl.setText("Search by:");

        searchByCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Code", "Model", "Brand" }));

        valueLbl.setText("Value:");

        valueTB.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                valueTBKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                valueTBKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                valueTBKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(valueTB);

        searchBtn.setText("Search");
        searchBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchBtnMouseClicked(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBtnMouseClicked(evt);
            }
        });

        errorLbl.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchByLbl)
                        .addGap(72, 72, 72)
                        .addComponent(valueLbl))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchByCB, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(errorLbl)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchBtn)
                        .addGap(18, 18, 18)
                        .addComponent(cancelBtn)))
                .addContainerGap(146, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchByLbl)
                    .addComponent(valueLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(searchByCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(errorLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchBtn)
                    .addComponent(cancelBtn))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBtnMouseClicked
        this.dispose();
    }//GEN-LAST:event_cancelBtnMouseClicked

    private void searchBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchBtnMouseClicked
        String valueText = this.valueTB.getText();
		if (valueText == null || valueText.isEmpty()) {
			this.errorLbl.setText("Value is required!");
			return;
		}
		
		this.dialogResult = true;
		this.value = valueText;
		this.dispose();
    }//GEN-LAST:event_searchBtnMouseClicked

    private void valueTBKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valueTBKeyPressed
        this.errorLbl.setText("");
    }//GEN-LAST:event_valueTBKeyPressed

    private void valueTBKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valueTBKeyReleased
        this.errorLbl.setText("");
    }//GEN-LAST:event_valueTBKeyReleased

    private void valueTBKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valueTBKeyTyped
        this.errorLbl.setText("");
    }//GEN-LAST:event_valueTBKeyTyped

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(MachineSearchByDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(MachineSearchByDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(MachineSearchByDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(MachineSearchByDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the dialog */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MachineSearchByDialog dialog = new MachineSearchByDialog(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel errorLbl;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton searchBtn;
    private javax.swing.JComboBox searchByCB;
    private javax.swing.JLabel searchByLbl;
    private javax.swing.JLabel valueLbl;
    private javax.swing.JTextPane valueTB;
    // End of variables declaration//GEN-END:variables
}
